package com.ruoyi.project.game.game.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.game.category.service.ICategoryService;
import com.ruoyi.project.game.game.domain.Game;
import com.ruoyi.project.game.game.service.IGameService;
import com.ruoyi.project.game.tag.service.ITagService;

/**
 * H5游戏Controller
 * 
 * @author shixiangwen
 * @date 2020-02-10
 */
@Controller
@RequestMapping("/game/game")
public class GameController extends BaseController {
	private String prefix = "game/game";

	@Autowired
	IGameService gameService;

	@Autowired
	ITagService tagService;

	@Autowired
	ICategoryService categoryService;

	@RequiresPermissions("game:game:view")
	@GetMapping()
	public String game() {
		return prefix + "/game";
	}

	/**
	 * 查询H5游戏列表
	 */
	@RequiresPermissions("game:game:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Game game) {
		startPage();
		List<Game> list = gameService.selectGameList(game);
		return getDataTable(list);
	}

	/**
	 * 导出H5游戏列表
	 */
	@RequiresPermissions("game:game:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(Game game) {
		List<Game> list = gameService.selectGameList(game);
		ExcelUtil<Game> util = new ExcelUtil<Game>(Game.class);
		return util.exportExcel(list, "game");
	}

	/**
	 * 新增H5游戏
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存H5游戏
	 */
	@RequiresPermissions("game:game:add")
	@Log(title = "H5游戏", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Game game) {
		return toAjax(gameService.insertGame(game));
	}

	/**
	 * 修改H5游戏
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {

		Game game = gameService.selectGameById(id);

		mmap.put("game", game);
		mmap.put("gameTags", tagService.selectTagsByGameId(id));
		mmap.put("assoTags", tagService.selectAssoTagsByGameId(id));
		mmap.put("gameCategorys", categoryService.selectCategorysByGameId(id));

		return prefix + "/edit";
	}

	/**
	 * 修改保存H5游戏
	 */
	@RequiresPermissions("game:game:edit")
	@Log(title = "H5游戏", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Game game) {
		return toAjax(gameService.updateGame(game));
	}

	/**
	 * 删除H5游戏
	 */
	@RequiresPermissions("game:game:remove")
	@Log(title = "H5游戏", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(gameService.deleteGameByIds(ids));
	}

	@GetMapping("/search")
	@ResponseBody
	public TableDataInfo search(String q) {
		startPage();
		Game game = new Game();
		game.setName(q);
		List<Game> list = gameService.selectGameList(game);
		return getDataTable(list);
	}

}
